package com.jml.zlhq.app.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jml.zlhq.app.R;
import com.jml.zlhq.app.adapter.IBaseAdapter;
import com.jml.zlhq.app.adapter.TeamLinGanAdapter;
import com.jml.zlhq.app.util.BaseTools;
import com.jml.zlhq.app.util.StringUtil;
import com.jml.zlhq.app.util.UIUtil;

public class AllWorkActivity extends BaseActivity {
	private ListView listView;
	private IBaseAdapter adapter;
	private List<JSONObject> listItems = new ArrayList<JSONObject>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.all_work);
		
		Bundle dataExtras = getIntent().getExtras();
		String jsonStr =  dataExtras==null||StringUtil.isEmpty(dataExtras.getString("json"))?"[]":dataExtras.getString("json");
		try {
			JSONArray jsonarr = new JSONArray(jsonStr);
			for (int i = 0;jsonarr!=null && i < jsonarr.length(); i++) {
				listItems.add(jsonarr.getJSONObject(i));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		LinearLayout leftLay = (LinearLayout) findViewById(R.id.content_header_left_lay);
		LinearLayout rightLay = (LinearLayout) findViewById(R.id.content_header_right_lay);
		leftLay.setOnClickListener(this);
		rightLay.setOnClickListener(this);
		AutoCompleteTextView centerEdit = (AutoCompleteTextView) findViewById(R.id.content_header_center_edit);
		centerEdit.setVisibility(View.GONE);
		ImageView leftImage = (ImageView) findViewById(R.id.content_header_left_img);
		leftImage.setImageResource(R.drawable.back_selector);
		ImageView roghtImage1 = (ImageView) findViewById(R.id.content_header_right_img);
		roghtImage1.setVisibility(View.INVISIBLE);
		TextView centerText = (TextView) findViewById(R.id.content_header_center_text);
		centerText.setText("所有作品");
		TextView rightText = (TextView) findViewById(R.id.content_header_right_text);
		rightText.setVisibility(View.INVISIBLE);
		TextView cityChosed = (TextView) findViewById(R.id.content_header_center_text2);
		cityChosed.setVisibility(View.GONE);
		
		
		listView = (ListView) findViewById(R.id.all_work_listview);
		//listView.setDividerHeight(UIUtil.dip2px(appContext, 10));
		adapter = new TeamLinGanAdapter(activity, listItems);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				int index = new Long(id).intValue();
				JSONObject json = listItems.get(index);
				if(!StringUtil.isEmpty(BaseTools.getFrom("_id",json))&&!BaseTools.getFrom("_id",json).equals("-1")){
					Bundle data = new Bundle();
					data.putString("id", BaseTools.getFrom("_id",json));
					UIUtil.openActivity(activity, WorkDetailActivity.class, data);
				}else{
					UIUtil.ToastMessage(activity, "未找到该作品信息");
				}
			}
		});
		loadData(urlParam);
		
	}
	
	@Override
	public void loadData(Map<String, String> param) { }

	@Override
	public void clearData() { }

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.content_header_left_lay:
			finish();
			break;
		default:
			break;
		}
	}

}
